package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.VendoutData;
import com.lkd.contract.VendoutResultContract;
import com.lkd.emq.Topic;
import com.lkd.entity.OrderEntity;
import com.lkd.service.OrderService;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Topic(TopicConfig.VMS_RESULT_TOPIC)
public class VendoutResultHandler implements MsgHandler {
    @Autowired
    private OrderService orderService;

    @Override
    public void process(String jsonMsg) throws IOException {
        VendoutResultContract vendoutResultContract = JsonUtil.getByJson(jsonMsg, VendoutResultContract.class);
        //机器编码
        String innerCode = vendoutResultContract.getInnerCode();
        //获取出货详情
        VendoutData vendoutData = vendoutResultContract.getVendoutData();
        boolean success = vendoutResultContract.isSuccess();
        //根据订单编号获取订单详情
        OrderEntity orderEntity = orderService.getByOrderNo(vendoutData.getOrderNo());
        if (success){
            //如果出货成功，则将订单状态设置为出货完成状态
            orderEntity.setStatus(VMSystem.ORDER_STATUS_VENDOUT_SUCCESS);
            orderService.updateById(orderEntity);
        }else {
            //如果出货结果异常，则将订单状态设置为出货失败状态
            orderEntity.setStatus(VMSystem.ORDER_STATUS_VENDOUT_FAIL);
            orderService.updateById(orderEntity);
        }
    }
}
